REM Kill Process
SET THISNAME=Alert on Close by Lectrode v1.0.054
TITLE %THISNAME%


:: -----SETTINGS-----
SET CUSTOMPROCESS1=sample.exe
SET CUSTOMPROCESS2=
SET CUSTOMPROCESS3=
SET CUSTOMPROCESS4=

REM Delay in seconds to wait before each check
SET CHECKDELAY=5

REM DEBUGGING (SET to "::" to turn on)
SET DEBUGGING=



:: -----MAIN-PROGRAM-----
:START

::INITIALIZE
%DEBUGGING%MODE CON COLS=60 LINES=20
SETLOCAL ENABLEDELAYEDEXPANSION
SET SPACES=25
SET GETTINGPARAMS=


CALL :HEADER
CALL :CALC_NAMES
CALL :ASK4Process
CALL :ASK4Action
CALL :CHECK4Process
CALL :DoAction
GOTO :START


:SLEEP
REM ------------------------PROGRAM SLEEP-------------------------
REM Program sleeps for %1 seconds
IF "%1"=="" SET pN=3
IF NOT "%1"=="" SET pN=%1
PING -n 2 -w 1000 127.0.0.1>NUL
PING -n %pN% -w 1000 127.0.0.1>NUL
GOTO :EOF
REM ------------------------END PROGRAM SLEEP---------------------


:CHECK4Process
CALL :HEADER
%DEBUGGING%ECHO Checking for %PROCESS% close...
CALL :SLEEP %CHECKDELAY%
TASKLIST | FIND "%PROCESS%">NUL
IF NOT ERRORLEVEL 1 GOTO :CHECK4Process
GOTO :EOF



:HEADER
%DEBUGGING%@%DEBUGGING%ECHO OFF
REM ---------------------PROGRAM STATUS-----------------------
CALL :CENTERTEXT 26 SHOWPROCESS %PROCESS%
CALL :CENTERTEXT 28 SHOWACTION %ACTION%

%DEBUGGING%CLS
%DEBUGGING%ECHO  **********************************************************
%DEBUGGING%ECHO  *                                                        *
%DEBUGGING%ECHO  *  ******  %THISNAME%   ******  *
%DEBUGGING%ECHO  *                                                        *
%DEBUGGING%ECHO  * http://code.google.com/p/useful-scripts-by-electrodexs *
%DEBUGGING%ECHO  *                                                        *
%DEBUGGING%ECHO  *--------------------------------------------------------*
%DEBUGGING%ECHO  *          PROCESS         ^|      Action on close:       *
%DEBUGGING%ECHO  *%SHOWPROCESS%^|%SHOWACTION% *
%DEBUGGING%ECHO  **********************************************************
%DEBUGGING%ECHO.
%DEBUGGING%ECHO.
GOTO :EOF

:ASK4Process
SET PROCESS=
CALL :HEADER
CALL :SHOW_CUSTOM_PROCESSES
SET usrInput=
SET /P usrInput=[] 
IF /I "%usrInput%"=="x" EXIT
IF NOT "%usrInput%"=="" SET PROCESS=%usrInput%
IF /I "%usrInput%"=="1" SET PROCESS=%CUSTOMPROCESS1%
IF /I "%usrInput%"=="2" SET PROCESS=%CUSTOMPROCESS2%
IF /I "%usrInput%"=="3" SET PROCESS=%CUSTOMPROCESS3%
IF /I "%usrInput%"=="4" SET PROCESS=%CUSTOMPROCESS4%
IF "%PROCESS%"=="" GOTO :ASK4Process
GOTO :EOF

:ASK4Action
SET ACTION=
CALL :HEADER
%DEBUGGING%ECHO What would you like this script to do when it 
%DEBUGGING%ECHO detects that %PROCESS% has ended?
%DEBUGGING%ECHO.
%DEBUGGING%ECHO Alert Only                [a]
%DEBUGGING%ECHO Shutdown                  [s]
%DEBUGGING%ECHO Restart                   [r]
%DEBUGGING%ECHO Log Off                   [l]
SET usrInput=
SET /P usrInput=[] 
IF /I "%usrInput%"=="a" SET ACTION=Alert Only
IF /I "%usrInput%"=="s" SET ACTION=Shutdown
IF /I "%usrInput%"=="r" SET ACTION=Restart
IF /I "%usrInput%"=="l" SET ACTION=Log Off
IF "%ACTION%"=="" GOTO :ASK4Action
GOTO :EOF

:DoAction
CALL :HEADER
%DEBUGGING%ECHO Process Close Detected!
%DEBUGGING%ECHO.
IF "%Action%"=="Alert Only" CALL :ALERTING
IF "%Action%"=="Shutdown" CALL :SHUTDOWN s
IF "%Action%"=="Restart" CALL :SHUTDOWN r
IF "%Action%"=="Log Off" CALL :SHUTDOWN l
GOTO :EOF

:ALERTING
%DEBUGGING%ECHO Press any key to start over...
PAUSE>NUL
GOTO :EOF

:SHUTDOWN
SET TODO=%1
%DEBUGGING%ECHO Attempting to %ACTION%...
%DEBUGGING%shutdown /f /%TODO% /t 00
PAUSE
GOTO :EOF




:CENTERTEXT
SET TTLSPACE=%1
SET VAR2SET=%2
SET TEXT=%3%
CALL :StrLength STRLEN %TEXT%
SET /A HALFSPACE=(TTLSPACE-STRLEN)/2

SET INCNUM=0
:ADDSPACEFRONT
SET /A INCNUM+=1
SET TEXT= %TEXT%
IF NOT %INCNUM% GEQ %HALFSPACE% GOTO :ADDSPACEFRONT

SET INCNUM=0
:ADDSPACEBACK
SET /A INCNUM+=1
SET TEXT=%TEXT% 
IF NOT %INCNUM% GEQ %HALFSPACE% GOTO :ADDSPACEBACK

SET /A NUMCHECK=(TTLSPACE-STRLEN)%%2
IF %NUMCHECK% GEQ 1 SET TEXT= %TEXT%
SET %VAR2SET%=%TEXT%
GOTO :EOF


:StrLength
::StrLength(retVal,string)
set #=%2%
set length=0
:stringLengthLoop
if defined # (set #=%#:~1%&set /A length += 1&goto stringLengthLoop)
set "%~1=%length%"
GOTO :EOF


:CALC_NAMES
CALL :StrLength C1 %CUSTOMPROCESS1%
CALL :StrLength C2 %CUSTOMPROCESS2%
CALL :StrLength C3 %CUSTOMPROCESS3%
CALL :StrLength C4 %CUSTOMPROCESS4%
SET LAST=%C1%
IF %C2% GEQ %LAST% SET LAST=%C2%
IF %C3% GEQ %LAST% SET LAST=%C3%
IF %C4% GEQ %LAST% SET LAST=%C4%
IF %LAST% GEQ %SPACES% SET /A SPACES=LAST+2
GOTO :EOF

:SHOW_CUSTOM_PROCESSES
SET SPACER=                                  !
SET SHOWCUSTOMPROCESS1=%CUSTOMPROCESS1%%SPACER%
SET SHOWCUSTOMPROCESS2=%CUSTOMPROCESS2%%SPACER%
SET SHOWCUSTOMPROCESS3=%CUSTOMPROCESS3%%SPACER%
SET SHOWCUSTOMPROCESS4=%CUSTOMPROCESS4%%SPACER%
SET SHOWEXIT=EXIT%SPACER%
%DEBUGGING%ECHO Please enter a number or the name of a process to kill:
IF NOT "%CUSTOMPROCESS1%"=="" %DEBUGGING%ECHO !SHOWCUSTOMPROCESS1:~0,%SPACES%![1]
IF NOT "%CUSTOMPROCESS2%"=="" %DEBUGGING%ECHO !SHOWCUSTOMPROCESS2:~0,%SPACES%![2]
IF NOT "%CUSTOMPROCESS3%"=="" %DEBUGGING%ECHO !SHOWCUSTOMPROCESS3:~0,%SPACES%![3]
IF NOT "%CUSTOMPROCESS4%"=="" %DEBUGGING%ECHO !SHOWCUSTOMPROCESS4:~0,%SPACES%![4]
%DEBUGGING%ECHO !SHOWEXIT:~0,%SPACES%![x]
GOTO :EOF